/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SaveOpen.java
 *
 * Created on 28.6.2011, 19:47:04
 */

package evidence.view;

import evidence.view.pomocneTridy.MyFileFilter;
import evidence.view.pomocneTridy.Util;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author Protheo
 */
public class SaveOpen extends javax.swing.JDialog {

    public static final long serialVersionUID = 1;
    public static final int OPENDLG = JFileChooser.OPEN_DIALOG;
    public static final int SAVEDLG = JFileChooser.SAVE_DIALOG;
    public static final int OPENDLGSET = OPENDLG+100;
    public static final int APPROVE = JFileChooser.APPROVE_OPTION;
    public static final int CANCEL = JFileChooser.CANCEL_OPTION;
    public static final int ERROROPT = JFileChooser.ERROR_OPTION;
    private static SaveOpen save = null;
    private static SaveOpen open = null;
    private static SaveOpen openSet = null;
    private static File soubor = null;
    private int retValue = CANCEL;

    /** Creates new form SaveOpen */
    private SaveOpen(int typ, Frame parent) {
        super(parent, true);
        initComponents();
        files.setDialogType(typ);
        files.setSelectedFile(soubor);
        files.setCurrentDirectory(((IPath)parent).getPath());
        files.removeChoosableFileFilter((files.getAcceptAllFileFilter()));
        this.addWindowListener(new MyWindowAdapter((View) parent));
    }

    public static SaveOpen getInstance(int typ, Frame parent) throws IllegalArgumentException{
        if (typ != OPENDLG && typ != SAVEDLG){
            throw new IllegalArgumentException("Neplatný typ dialogu");
        }
        switch (typ) {
            case OPENDLG : {
                if (open == null){
                    open = new SaveOpen(typ, parent);
                }
                return open;
            }
            case OPENDLGSET : {
                if (openSet == null){
                    openSet = new SaveOpen(typ, parent);
                }
                return openSet;
            }
            case SAVEDLG : {
                if (save == null){
                    save = new SaveOpen(typ, parent);
                }
                return save;
            }
            default: return open;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        files = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        files.setFileFilter(new MyFileFilter());
        files.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClick(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(files, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(files, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClick(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClick
        if (evt.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)){
            this.retValue = APPROVE;
            soubor = files.getSelectedFile();
        } else if (evt.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)){
            this.retValue = CANCEL;
        }
        this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_buttonClick

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser files;
    // End of variables declaration//GEN-END:variables

    private class MyWindowAdapter extends WindowAdapter {

        private View okno = null;

        public MyWindowAdapter(View okno) {
            this.okno = okno;
        }

        @Override
        public void windowClosing(WindowEvent e) {
            SaveOpen dotaz = (SaveOpen) e.getWindow();
            File soubor = dotaz.getFile();
            if (dotaz.getRetValue() == SaveOpen.APPROVE){
                if(!dotaz.accept(soubor)){
                    if (Util.getExtension(soubor)==null){
                        soubor = new File(soubor.getPath()+".evi");
                    } else {
                        return;
                    }
                }
                if (dotaz.getDlgType() == SaveOpen.OPENDLG){
                    this.okno.open(soubor);
                } else {
                    this.okno.save(soubor);
                }
            }
            super.windowClosing(e);
            dotaz.setVisible(false);
        }
    }

    public File getFile() {
        return soubor;
    }

    public int getRetValue() {
        return retValue;
    }

    public int getDlgType() {
        return this.files.getDialogType();
    }

    public boolean accept(File f){
        return files.accept(f);
    }

    private static void reset(File f){

    }
}
